﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Biz;
using MockPrjTeam2.Entity;

namespace MockPrjTeam2.Pages.Forms
{
    public partial class AddBussinessType : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Convert.ToInt32(Request.QueryString["BussinessId"]) > 0)
                {
                    btAdd.Visible = false;
                    LoadAddressById();
                }
                else
                {
                    btUpdate.Visible = false;                    
                }

            }
        }
        private void LoadAddressById()
        {
            int id = Convert.ToInt32(Request.QueryString["BussinessId"]);
            var biz = new BussinessBiz();
            var bussiness = new Bussiness();
            bussiness = biz.getById(id);
            txtBussName.Text = bussiness.BusinessName;
            txtSic.Text = bussiness.SicCode;

        }
        protected void btAdd_Click(object sender, EventArgs e)
        {
            var biz = new BussinessBiz();
            var bussiness = new Bussiness
            {
                SicCode = txtSic.Text.ToString(),
                BusinessName = txtBussName.Text.ToString()                
            };
            bool result = biz.addBussinessType(bussiness);
            if (result == true)
            {
                Response.Redirect("../Views/SearchBussiness.aspx");
            }
        }

        protected void btUpdate_Click(object sender, EventArgs e)
        {
            var biz = new BussinessBiz();
            int id = Convert.ToInt32(Request.QueryString["BussinessId"]);
            var bussiness = new Bussiness
            {
                SicCode = txtSic.Text.ToString(),
                BusinessName = txtBussName.Text.ToString(),                               
                BusinessId = id
            };
            bool result = biz.updateBussinessType(bussiness);
            if (result == true)
            {
                Response.Redirect("../Views/SearchBussiness.aspx");
            }
        }

        private void ClearText()
        {
            txtBussName.Text = string.Empty;            
            txtSic.Text = string.Empty;
        }

        protected void btClear_Click(object sender, EventArgs e)
        {
            ClearText();
        }
    }
}